/*
 * @Author: xiaosihan 
 * @Date: 2022-06-08 10:41:22 
 * @Last Modified by: xiehangyun
 * @Last Modified time: 2025-07-24 20:03:07
 */

import express from 'express';
import nodemailer from "nodemailer";
import ServiceBase from './ServiceBase';

// 邮件服务
class EmailServer extends ServiceBase {

    constructor(req: express.Request = {} as express.Request, res: express.Response = {} as express.Response) {
        super(req, res);
    }

    transporter = nodemailer.createTransport({
        host: 'smtp.qq.com',
        secure: true,
        auth: {
            user: '389652405@qq.com',//输入你开启SMTP服务的QQ邮箱        
            pass: 'jleavvianeuzbidc'
        }
    });

    // 发送邮件
    send_mail = async (params: Record<string, any>) => {
        // 参数校验
        this.verifyParams(['to', 'subject', 'html']);

        const { to, subject, html } = params;

        try {
            const data = await this.transporter.sendMail({
                from: '389652405@qq.com',
                to,
                subject,
                html
            });

            console.log("邮件发送成功:", data);
            return data;
        } catch (error) {
            console.error("邮件发送失败:", error);
            throw new Error('邮件发送失败');
        }
    };

}

export default EmailServer;